﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Web.Script.Serialization;

namespace U9API.Pub
{
    public static class Common
    {
        static DateTime mindate = new DateTime(2000, 1, 1);
        public static string ToJson(this object t)
        {
            if (t == null)
            {
                return string.Empty;
            }

            JavaScriptSerializer js = new JavaScriptSerializer();
            js.MaxJsonLength = Int32.MaxValue;
            return js.Serialize(t);
        }
        /// <summary>
        /// json格式转换
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="strJson"></param>
        /// <returns></returns>
        public static T FromJson<T>(string strJson) where T : class
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Deserialize<T>(strJson); //反序列化
        }
        public static Boolean IsNullOrEmpty(this object obj)
        {
            if (obj == null)
            {
                return true;
            }
            String s = obj.ToString();
            return String.IsNullOrEmpty(s);
        }
        public static string ObjToString(this object obj)
        {
            if (obj == null)
            {
                return "";
            }
            else
            {
                return obj.ToString();
            }
        }

        public static int BoolToInt32(this bool obj)
        {
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return obj ? 1 : 0;
            }
        }

       
        public static string ObjToStringNull(this object obj)
        {
            if (obj == null||obj==DBNull.Value)
            {
                return null;
            }
            else
            {
                return obj.ToString();
            }
        }
        public static string ObjToStringTrim(this object obj)
        {
            return obj.ObjToString().Trim();
        }
        public static decimal ObjToDecimal(this object obj)
        {
            if (obj == null)
            {
                return 0;
            }
            else
            {
                try
                {
                    return Convert.ToDecimal(obj);
                }
                catch
                {
                    return 0;
                }
            }
        }
        public static double ObjToDouble(this object obj)
        {
            if (obj == null)
            {
                return 0;
            }
            else
            {
                try
                {
                    return Convert.ToDouble(obj);
                }
                catch
                {
                    return 0;
                }
            }
        }
        public static float ObjToFloat(this object obj)
        {
            if (obj == null)
            {
                return 0;
            }
            else
            {
                try
                {
                    return Convert.ToSingle(obj);
                }
                catch
                {
                    return 0;
                }
            }
        }

        public static DateTime ObjToDate(this object obj)
        {
            if (obj == null)
            {
                return DateTime.MinValue;
            }
            else
            {
                try
                {
                    return Convert.ToDateTime(obj);
                }
                catch
                {
                    return DateTime.MinValue;
                }
            }
        }
         
        public static DateTime ObjToDateTime(this object obj)
        {
            if (obj == null)
            {
                return mindate;
            }
            else
            {
                try
                {
                    return Convert.ToDateTime(obj);
                }
                catch
                {
                    return mindate;
                }
            }
        }
        public static DateTime? ObjToDateTimeNull(this object obj)
        {
            if (obj == null)
            {
                return null;
            }
            else
            {
                try
                {
                    return Convert.ToDateTime(obj);
                }
                catch
                {
                    return null;
                }
            }
        }
        public static int ObjToInt32(this object obj)
        {
            if (obj == null)
            {
                return 0;
            }
            else
            {
                try
                {
                    return Convert.ToInt32(obj);
                }
                catch
                {
                    return 0;
                }
            }
        }
        public static int? ObjToInt32Null(this object obj)
        {
            if (obj == null)
            {
                return null;
            }
            else
            {
                try
                {
                    return Convert.ToInt32(obj);
                }
                catch
                {
                    return null;
                }
            }
        }
        public static long ObjToInt64(this object obj)
        {
            if (obj == null)
            {
                return -1;
            }
            else
            {
                try
                {
                    return Convert.ToInt64(obj);
                }
                catch
                {
                    return -1;
                }
            }
        }
        public static Boolean ObjToBoolean(this object obj)
        {
            if (obj == null)
            {
                return false;
            }
            else
            {
                try
                {
                    return Convert.ToBoolean(obj);
                }
                catch
                {
                    return false;
                }
            }
        }
        public static String GetNewID()
        {
            return Guid.NewGuid().ToString().Substring(0, 8) + DateTime.Now.ToString("yyMMddHHmmss");
        }
        public static String GetKey(String partid, string routingcode)
        {
            return GetKey(new List<string>() { partid, routingcode });
        }
        public static String GetKey(String mobody,String routingcode,string itemcode, string free1, string free2, string free3)
        {
            //StringBuilder builder = new StringBuilder();
            //builder.Append(routingcode);
            //builder.Append("_");
            //builder.Append(itemcode);
            //builder.Append("_");
            //builder.Append(free1.ObjToString());
            //builder.Append("_");
            //builder.Append(free2.ObjToString());
            //builder.Append("_");
            //builder.Append(free3.ObjToString());
            //return builder.ToString();
            return GetKey(new List<string>() { mobody,routingcode,itemcode, free1, free2, free3 });
        }
        public static String GetKey(string itemcode, string free1, string free2, string free3)
        {
            //StringBuilder builder = new StringBuilder();
            //builder.Append(itemcode);
            //builder.Append("_");
            //builder.Append(free1.ObjToString());
            //builder.Append("_");
            //builder.Append(free2.ObjToString());
            //builder.Append("_");
            //builder.Append(free3.ObjToString());
            //return builder.ToString();
            return GetKey(new List<string>() { itemcode, free1, free2, free3, String.Empty, String.Empty, 
                String.Empty, String.Empty, String.Empty, String.Empty,String.Empty });
        }
        public static String GetKey(string itemcode, params string[] free)
        {
            //StringBuilder builder = new StringBuilder();
            //builder.Append(itemcode);
            //builder.Append("_");
            //builder.Append(free1.ObjToString());
            //builder.Append("_");
            //builder.Append(free2.ObjToString());
            //builder.Append("_");
            //builder.Append(free3.ObjToString());
            //return builder.ToString();
            List<String> temp = new List<string>();
            temp.Add(itemcode);
            temp.AddRange(free);
            return GetKey(temp);
        }
        public static String GetKey(List<String> strs)
        {
            StringBuilder builder = new StringBuilder();
            foreach (string str in strs)
            {
                if (!str.IsNullOrEmpty())
                {
                    builder.Append(str);
                }
                builder.Append("_");
            }
            if (builder.Length > 0)
            {
                builder.Remove(builder.Length - 1, 1);
            }
            return builder.ToString();
        }




    }
}
